<?php

//结果处理函数
function tt_get_json_data($result, $raw)
{
    if($raw === false){
        $text = $result['post_name'];
        if($text == 'url'){
            $result['post_content'] = '<a href="'.$result['post_content'].'" target="_blank">'.$result['post_content'].'</a>';
        }
        $text = $result['post_content'];
        //$result['post_content'] = MarkdownExtra::defaultTransform($text);
    }
    $result = json_encode($result);
    return $result;
}

function tt_url_encode($url)
{
    $url=rawurlencode($url);
    return $url;
}


//安全过滤函数
function safe($s)
{
    if (get_magic_quotes_gpc()) {
        $s = stripslashes($s);
    }
    $s = mysql_real_escape_string($s);
    return $s;
}

//根据时间获得sid
function get_tmp_sid()
{
        $time_stamp = (int)(time() / 10);
        $s = (string)$time_stamp . "ticktips";
        return md5($s);
}

//根据时间校验sid
function cmp_tmp_sid($sid)
{
    return true;//暂时不判断时间sid，以方便浏览器的后退键的使用
    $sid = (string) $sid;
    //这里让10s之内有效
    $time_stamp = (int)(time() / 10);

    $s = (string)$time_stamp . "ticktips";
    if(md5($s) == $sid){
        return true;
    }

    //让上一个10s也合法
    $s = (string)($time_stamp - 1) . "ticktips";
    if(md5($s) == $sid){
        return true;
    }
    return false;
}

function get_page($page){
    $page = 0 + (int)$page;
    $page = $page == 0 ? 1 : $page;
    return $page;
}

function getGET($k){
  return getFromMap($_GET, $k, '');
}

function getPOST($k){
  return getFromMap($_POST, $k, '');
}

function getFromMap($m, $k, $default){
  if(!isset($m[$k])){
    return $default;
  }
  return $m[$k];
}


function get_result($sql){
  $tmp = array();
  $sql=mysql_query($sql);
  while(($result = mysql_fetch_array($sql, MYSQL_ASSOC))!=false){
    array_push($tmp, $result);
  }
  return $tmp;
}

function getUserInfo($userid, $passwd)
{
  $sql = "SELECT `id`, `name` from `user` WHERE ((`id`='$userid' OR `mail`='$userid') AND `password`='$passwd')";
  $result = get_result($sql, $linkUser);
  return $result;
}

function checkLogin(){  
  //var_dump($_COOKIE);
  // TODO there is a safety problem
  if(empty($_SESSION['user_info'])){    //检查一下session是不是为空  
    if(empty($_COOKIE['userid']) || empty($_COOKIE['password'])){  //如果session为空，并且用户没有选择记录登录状  
      header("location:account.php?req_url=".$_SERVER['REQUEST_URI']);  //转到登录页面，记录请求的url，登录后跳转过去，用户体验好。  
    }else{   //用户选择了记住登录状态  
      $user = getUserInfo($_COOKIE['userid'],$_COOKIE['password']);   //去取用户的个人资料  
      if(empty($user)){    //用户名密码不对没到取到信息，转到登录页面  
        header("location:login.php?req_url=".$_SERVER['REQUEST_URI']);  
      }else{  
        $_SESSION['user_info'] = $user;   //用户名和密码对了，把用户的个人资料放到session里面  
      }  
    }  
  }  
}


function sendpost($url, $data){
  //先解析url
  $url = parse_url($url);
  $url_port = !isset($url['port']) ? (($url['scheme'] == 'http') ? 80 : 443) : $url['port'];
  if (!$url)
  {
    return "{code:-1, msg:'url error'}";
  }
  //将参数拼成URL key1=value1&key2=value2 的形式
  $encoded = "";
  while (list($k, $v) = each($data))
  {
    $encoded .= ($encoded ? '&' : '');
    $encoded .= rawurlencode($k)."=".rawurlencode($v);
  }
  $len = strlen($encoded);
  //拼上http头
  $out = "POST ".$url['path'].(isset($url['query']) ? ('?'.$url['query']) : '')." HTTP/1.1\r\n";
  $out .= "Host:".$url['host']."\r\n";
  $out .= "Content-type: application/x-www-form-urlencoded\r\n";
  $out .= "Connection: Close\r\n";
  $out .= "Content-Length: $len\r\n";
  $out .= "\r\n";
  $out .= $encoded."\r\n";
  //打开一个sock
  $fp = @fsockopen($url['host'], $url_port);
  $line = "";
  if(!$fp){
    return "{code:-2, msg:'$errstr($errno)'}";
  }else{
    fwrite($fp,$out);
    while (!feof($fp))
    {
      $line .= fgets($fp, 2048);
    }
  }
  //去掉头文件
  if ($line)
  {
    $body = stristr($line, "\r\n\r\n");
    $body = substr($body, 4, strlen($body));
    $a = strpos($body, "{");
    $b = strrpos($body, "}");
    if ($a != -1 && $b !=-1 && $a<$b){
      $body = substr($body, $a, $b-$a+1);
    }
    $line = $body;
  }
  fclose($fp);
  return $line;
}


?>
